package com.sicnustray.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sicnustray.common.Result;
import com.sicnustray.utils.annotation.MyLog;
import com.sicnustray.entity.Syslog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sicnustray.service.ISyslogService;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yl
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/syslog")
public class SyslogController {
    
        @Resource
        private ISyslogService syslogService;

        // 新增或者更新
        @MyLog(value = "新增或者更新操作日志")
        @PostMapping
        public Result save(@RequestBody Syslog syslog) {
                return Result.success(syslogService.saveOrUpdate(syslog));
                }

        @RequiresPermissions("操作日志")
        @MyLog(value = "单个删除操作日志")
        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(syslogService.removeById(id));
                }

        @RequiresPermissions("操作日志")
        @MyLog(value = "批量操作日志")
        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(syslogService.removeByIds(ids));
                }

        @RequiresPermissions("操作日志")
        @MyLog(value = "获取所有操作日志")
        @GetMapping
        public Result findAll() {
                return Result.success(syslogService.list());
                }

        @RequiresPermissions("操作日志")
        @MyLog(value = "单个获取操作日志")
        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(syslogService.getById(id));
                }

        @RequiresPermissions("操作日志")
        @MyLog(value = "分页查询操作日志")
        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String username) {
                QueryWrapper<Syslog> queryWrapper = new QueryWrapper<>();
            //    queryWrapper.orderByDesc("create_date");
                if (!"".equals(username)) {
                        queryWrapper.like("username", username);
                }
                return Result.success(syslogService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }

        }

